package com.sangmo.fts.process.factoring.calculator;

import com.sangmo.boot.framework.common.utils.ApplicationUtils;
import com.sangmo.boot.framework.data.json.context.KeyValue;
import com.sangmo.boot.framework.data.json.load.Calculator;
import com.sangmo.fts.process.factoring.model.FfLoan;
import com.sangmo.fts.process.factoring.model.FfRepayment;
import com.sangmo.fts.process.factoring.repo.FfRepaymentRepo;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

@Component
public class FfLoanRepaymentAmountCalculator implements Calculator<FfLoan>, FfLoan.IFfLoanRepaymentAmountCalculator {

    @Override
    public Object calc(FfLoan bean, Collection<KeyValue> exts) {
        FfRepaymentRepo ffRepaymentRepo = ApplicationUtils.getBean(FfRepaymentRepo.class);
        List<FfRepayment> repayments = ffRepaymentRepo.findByLoanId(bean.getId());
        BigDecimal repaymentAmount = BigDecimal.ZERO;
        for (FfRepayment repayment : repayments) {
            repaymentAmount = repaymentAmount.add(repayment.getAmount());
        }
        return repaymentAmount;
    }
}
